package czy.demo.config.shiro;

import org.apache.shiro.biz.web.filter.authc.listener.LogoutListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/* shiro自定义过滤器配置 */
@Configuration
public class FilterConfig {

    @Autowired
    private DefaultLogoutFilter logoutFilter;

    /* 注册自定义登出过滤器 */
    @Bean("logout")
    public FilterRegistrationBean<DefaultLogoutFilter> logoutFilter(List<LogoutListener> logoutListeners){

        FilterRegistrationBean<DefaultLogoutFilter> registration = new FilterRegistrationBean<>();

        logoutFilter.setLogoutListeners(logoutListeners);
        logoutFilter.setPostOnlyLogout(true);

        registration.setFilter(logoutFilter);
        /* shiro的过滤器通过ShiroFilterFactoryBean收集
         * 并不注册于servlet容器中
          * */
        registration.setEnabled(false);
        return registration;
    }

}
